package com.oa.ylms.apps.business.plan.impl;

import java.util.List;

import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.business.plan.IMessageInfoService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.message.IMessageInfoStates;
import com.oa.ylms.apps.dao.plan.IMessageInfoDao;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.plan.MessageInfoValue;

public class MessageInfoServiceImpl implements IMessageInfoService {
	private IMessageInfoDao infoDao;
	private IStaffInfoService staffInfoService;
	private MessageInfoValue messageInfo;
	

	@Override
	public int addMessageInfo(String title, String contents, int staffId) throws Exception {
		messageInfo.setToStaffId(staffInfoService.queryByStaffId(staffId));
		if(messageInfo.getToStaffId() != null){
			messageInfo.setTitle(title);
			messageInfo.setContents(contents);
			messageInfo.setCreateTime(DateUtil.toDate(DateUtil.getCurrentDate()));
			messageInfo.setStates(IDEDStates.DISABLE);
			return infoDao.addMessageInfo(messageInfo);
		}
		return 0;
	}

	@Override
	public int countTPage(int staffId, Integer states, int rows)
			throws Exception {
		messageInfo.setToStaffId(staffInfoService.queryByStaffId(staffId));
		messageInfo.setStates(states);
		return infoDao.countTPage(messageInfo, rows);
	}

	@Override
	public boolean deleteMessageInfo(int messageId) throws Exception {
		messageInfo = queryByMessageId(messageId);
		if(messageInfo != null){
			return infoDao.deleteMessageInfo(messageInfo);
		}
		return false;
	}

	@Override
	public MessageInfoValue queryByMessageId(int messageId) throws Exception {
		messageInfo.setMessageId(messageId);
		return infoDao.queryByMessageId(messageInfo);
	}

	@Override
	public List<MessageInfoValue> queryMessageInfoWithPage(int staffId, Integer states,
			int rows, int pageNo) throws Exception {
		messageInfo.setToStaffId(staffInfoService.queryByStaffId(staffId));
		messageInfo.setStates(states);
		return infoDao.queryMessageInfoWithPage(messageInfo, rows, pageNo);
	}

	@Override
	public boolean read(int messageId) throws Exception {
		messageInfo = queryByMessageId(messageId);
		if(messageInfo != null){
			messageInfo.setStates(IMessageInfoStates.READ);
			return infoDao.modifyMessageInfo(messageInfo);
		}
		return false;
	}
	
	@Override
	public int getTotalCount(StaffInfoValue staffInfo) throws Exception {
		messageInfo.setToStaffId(staffInfo);
		messageInfo.setStates(IMessageInfoStates.UNDEFINED);
		return infoDao.getCount(messageInfo);
	}

	@Override
	public int getUnreadCount(StaffInfoValue staffInfo) throws Exception {
		messageInfo.setToStaffId(staffInfo);
		messageInfo.setStates(IMessageInfoStates.UNREAD);
		return infoDao.getCount(messageInfo);
	}
	

	public IMessageInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IMessageInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public IStaffInfoService getStaffInfoService() {
		return staffInfoService;
	}

	public void setStaffInfoService(IStaffInfoService staffInfoService) {
		this.staffInfoService = staffInfoService;
	}

	public MessageInfoValue getMessageInfo() {
		return messageInfo;
	}

	public void setMessageInfo(MessageInfoValue messageInfo) {
		this.messageInfo = messageInfo;
	}

	public int getTotalNumber() throws Exception {
		// TODO Auto-generated method stub
		return infoDao.getTotalNumber();
	}


}
